//
//  PhotosTVController.m
//  AndonImagePicker
//
//  Created by 董文博 on 15/1/7.
//  Copyright (c) 2015年 OA. All rights reserved.
//

#import "AndonGroupTVController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "PhotoSelectCVController.h"

@interface AndonGroupTVController ()
@property (nonatomic,strong) NSMutableArray   *groupArrM;
@property (nonatomic,strong) ALAssetsLibrary  *assetsLibrary;
@end

@implementation AndonGroupTVController
-(void)setMaxSelectNum:(NSInteger)maxSelectNum
{
    if ([[self.navigationController viewControllers] count] == 2) {
        PhotoSelectCVController *pscvc = [[self.navigationController viewControllers] objectAtIndex:1];
        pscvc.maxSelectNum = maxSelectNum;
        }
    _maxSelectNum = maxSelectNum;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择相册";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:nil action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(cancle)];
    
    self.groupArrM = [[NSMutableArray alloc] init];
    
    self.assetsLibrary = [[ALAssetsLibrary alloc] init];
    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if(group){
            [self.groupArrM addObject:group];
            
        }else{
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"%@",error.localizedFailureReason);
    }];
    
}

-(void)cancle{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.groupArrM.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"photo"];
    ALAssetsGroup *group = (ALAssetsGroup *)self.groupArrM[indexPath.row];
    CGImageRef imageRef = [group posterImage];
    cell.imageView.image = [UIImage imageWithCGImage:imageRef];
    cell.textLabel.text = [group valueForProperty:ALAssetsGroupPropertyName];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",[group numberOfAssets]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;
    CGSize itemSize = CGSizeMake(100, 100);
    if (screenW == 320) {
        itemSize = CGSizeMake(70, 70);
    }else if(screenW == 375){
        itemSize = CGSizeMake(83, 83);
    }else if(screenW == 414){
        itemSize = CGSizeMake(93, 93);
    }
    layout.itemSize = itemSize;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    PhotoSelectCVController *pscvc = [[PhotoSelectCVController alloc] initWithCollectionViewLayout:layout];
    pscvc.maxSelectNum = self.maxSelectNum;
    pscvc.group = self.groupArrM[indexPath.row];
    [self.navigationController pushViewController:pscvc  animated:YES];
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
